package com.example.midushop.controller;

import com.example.midushop.entity.category.CategoryOne;
import com.example.midushop.entity.category.CategoryThree;
import com.example.midushop.entity.category.CategoryTwo;
import com.example.midushop.service.brand.impl.BrandCategoryServiceImpl;
import com.example.midushop.service.category.impl.CategoryOntServiceImpl;
import com.example.midushop.service.category.impl.CategoryThreeServiceImpl;
import com.example.midushop.service.category.impl.CategoryTwoServiceImpl;
import com.example.midushop.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 分类信息接口控制器
 */
@RestController
@RequestMapping("/api/findCategory")
public class CategoryController {
    @Autowired
    CategoryOntServiceImpl categoryOntService;
    @Autowired
    CategoryThreeServiceImpl categoryThreeService;
    @Autowired
    CategoryTwoServiceImpl categoryTwoService;
    @Autowired
    BrandCategoryServiceImpl brandCategoryService;

    /**
     * 根据编号查询一级分类信息，若不传编号则查询所有数据
     * @param categoryOne_id
     * @return
     */
    @GetMapping("/getCategoryOne")
    public R findCategoryOne(@RequestParam(required = false,defaultValue = "") long categoryOne_id,
                             @RequestParam(required = false,defaultValue = "") String categoryOne_name){
        return R.ok(categoryOntService.findCategoryOneAll(categoryOne_id,categoryOne_name));
    };

    /**
     * 根据一级分类ID,查询所有二级分类（不传二级分类ID时默认查询所有）
     * @param categoryOne_id
     * @param categoryTwo_id
     * @return 返回List集合
     */
    @GetMapping("/getCategoryTwo")
    public R findCategoryTwo(@RequestParam(required = false,defaultValue = "") long categoryOne_id,
                             @RequestParam(required = false,defaultValue = "") long categoryTwo_id,
                             @RequestParam(required = false,defaultValue = "") String categoryTwo_name){
        return R.ok(categoryTwoService.findCategoryTwoAll(categoryOne_id,categoryTwo_id,categoryTwo_name));
    };

    /**
     * 根据二级分类编号查询三级分类信息，如果不传三级分类ID则返回所有结果
     * @param categoryTwo_id
     * @param categoryThree_id
     * @return 返回List集合，三级分类信息
     */
    @GetMapping("/getCategoryThree")
    public R findCategoryThree(@RequestParam(required = false) long categoryTwo_id,
                               @RequestParam(required = false) long categoryThree_id,
                               @RequestParam(required = false,defaultValue = "") String categoryThree_name){
        return R.ok(categoryThreeService.findCategoryThreeAll(categoryTwo_id,categoryThree_id,categoryThree_name));
    };

    /**
     * 根据一级分类ID,添加二级分类
     * @param CategoryOne_id 一级分类ID
     * @param CategoryTwo_name 二级分类名称
     * @return 返回受影响行数
     * @throws IOException
     */
    @PostMapping("/addCategoryTwo")
    public R addCategoryTwo(@RequestParam(required = false) long CategoryOne_id,
                            @RequestParam(required = false,defaultValue = "") String CategoryTwo_name) throws IOException {
        return R.ok(categoryTwoService.addCategoryTwo(CategoryOne_id,CategoryTwo_name,1,new Date(),new Date()));
    };

    /**
     * 根据二级分类ID,添加三级分类
     * @param CategoryTwo_id 二级分类ID
     * @param CategoryThree_name 三级分类名称
     * @param create_time 创建时间
     * @param update_time 更新时间
     * @return 返回受影响行数
     * @throws IOException
     */
    @PostMapping("/addCategoryThree")
    public R addCategoryThree(@RequestParam(required = false) long CategoryTwo_id,
                              @RequestParam(required = false,defaultValue = "") String CategoryThree_name,
                              @RequestParam(required = false) Date create_time,
                              @RequestParam(required = false) Date update_time) throws IOException{
        return R.ok(categoryThreeService.addCategoryThree(CategoryTwo_id,CategoryThree_name,1,create_time,update_time));
    };

    /**
     * 根据品牌ID以及一级分类ID绑定品牌分类信息
     * @param categoryone_id 一级分类ID
     * @param brand_id 品牌ID
     * @return 返回受影响行数
     */
    @PostMapping("/addBrandCategory/categoryone_id={categoryone_id}&&brand_id={brand_id}")
    public R addBrandCategory(@PathVariable("categoryone_id") long categoryone_id,
                              @PathVariable("brand_id") long brand_id){
        return R.ok(brandCategoryService.addBrandCategory(categoryone_id,brand_id));
    }

    /**
     * 查询品牌一级分类
     * @param categoryone_id
     * @param brand_id
     * @return 受影响行数
     */
    @GetMapping("/getBrand_Category/categoryone_id={categoryone_id}&&brand_id={brand_id}")
    public R getBrand_Category(@PathVariable("categoryone_id") long categoryone_id,
                               @PathVariable("brand_id") long brand_id){
        System.out.println(brand_id);
        return R.ok(brandCategoryService.getBrand_Category(categoryone_id,brand_id));
    }
}
